package rabbit.deal;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @Author: Base64工具类
 * @Description:
 * @Date: 2018/7/1 10:41
 */

public class Base64Helper {

	/**
	 * Base64编码
	 *
	 * @param data
	 * @return
	 */
	public static byte[] encode(byte[] data) {
		return Base64.getEncoder().encode(data);
	}


	/**
	 * Base64编码
	 *
	 * @param data
	 * @param charsetName
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static byte[] encode(String data, String charsetName) throws UnsupportedEncodingException {
		return Base64.getEncoder().encode(data.getBytes(charsetName));
	}

	/**
	 * Base64编码
	 *
	 * @param data
	 * @return
	 */
	public static String encodeToString(byte[] data) {
		return Base64.getEncoder().encodeToString(data);
	}

	/**
	 * Base64编码
	 *
	 * @param data
	 * @param charsetName
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String encodeToString(String data, String charsetName) throws UnsupportedEncodingException {
		return Base64.getEncoder().encodeToString(data.getBytes(charsetName));
	}

	/**
	 * Base64解码
	 *
	 * @param data
	 * @param charsetName
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static byte[] decode(String data, String charsetName) throws UnsupportedEncodingException {
		return Base64.getDecoder().decode(data.getBytes(charsetName));
	}

	/**
	 * Base64解码
	 *
	 * @param data
	 * @return
	 */
	public static byte[] decode(byte[] data) {
		return Base64.getDecoder().decode(data);
	}
}
